﻿using System;
using System.Collections.Generic;
using MedicService.Domain.Print.Template;
using MedicService.Server.Factory;
using System.Linq;

namespace MedicService.Domain.Print
{
    public class PrintProfileProtocol : PrintEntity{
        private PrintProfileProtocol() { }

        public PrintProfileProtocol(PrintTemplate<PrintProfileProtocol> template, Doctor doctor) {
            Init(template);
            doctor = new DoctorFactory().Load(doctor.Id);
            Replace(template, doctor);

            int index = 0;

            do {
                int start = this.Html.IndexOf("[Members]", index);
                int end = this.Html.IndexOf("[/Members]") + 10;
                index = end;

                if (start >= 0) {
                    string s = this.Html.Substring(start, end - start);
                    string members = s.Replace("[Members]", "").Replace("[/Members]", "");
                    GroupExp groupExp = doctor.GroupExp;
                    List<GroupExpMember> memberList = new GroupExpMemberFactory().GetList(groupExp);
                    string replace = "";
                    foreach (var member in memberList) {
                        replace += Replace(members, member);
                    }
                    this.Html = this.Html.Replace(s, replace);
                }
            } while (index > 9);
        }
    }
}